#  -*- coding:utf-8 -*- 
"""
@ author: 罗金盛
@ time: 2024/3/6 
@ file: 10.with 自开合原理.py

"""

# with as f : f
# enter exit
class File:
    def __init__(self,filename):
        self.filename=open(filename,'r',encoding='utf-8')

    def __enter__(self):
        print('enter file')
        return self

    def __exit__(self,exc_type,exc_value,exc_tb):
        print('exit file')
        self.filename.close()

    def read(self):
        return self.filename.read()

x=File('ljs.txt')
print(x.read())

with File('ljs.txt') as f:
    print(f.read())